1) pid_t fork(void) returns the pid of the child for a parent fork and for a child fork it returns 0. Any error yields -1.
2) pid_t getpid(void) returns the pid of the current process.
3) pid_t getppid(void) returns the pid of the parent process.
4) The process image is everything you get from the program executable and load into memory, plus everything that gets added or modified at load time. The address space is all its virtual addresses, and whatever is in them.
5) It gets replaced with the new requested process from the exec function call.
6) The exec function has the following versions or family:
(A thing to note is that the functions in this family differ in how you specify the arguments,
but otherwise they all do the same thing. They are declared in the
header file unistd.h.)
- int execv -> Executes the file named by filename as a new process image.
- int execl -> Similar to execv, but the argv string are specified individually instead of as an array. A null pointer must be passed as the last such argument.
- int execve -> This is similar to execv, but permits you to specify the environment
for the new program explicitly as the env argument. This should
be an array of strings in the same format as for the environ
variable.
- int fexecve -> This is similar to execve, but instead of identifying the program
executable by its pathname, the file descriptor fd is used. The
descriptor must have been opened with the O_RDONLY flag or (on
Linux) the O_PATH flag.
- int execle -> This is similar to execl, but permits you to specify the
environment for the new program explicitly. The environment argument is
passed following the null pointer that marks the last argv
argument, and should be an array of strings in the same format as for
the environ variable.
- int execvp -> The execvp function is similar to execv, except that it
searches the directories listed in the PATH environment variable to find the full file name of a
file from filename if filename does not contain a slash. This function is useful for executing system utility programs, because
it looks for them in the places that the user has chosen. Shells use it
to run the commands that users type.
- int execlp -> This function is like execl, except that it performs the same
file name searching as the execvp function.
7) The pid_t waitpid (pid_t pid, int *status-ptr, int options) function is used to request status information from a
child process whose process ID is pid. Normally, the calling
process is suspended until the child process makes status information
available by terminating. If status information for a child process is available immediately, this
function returns immediately without waiting. If more than one eligible
child process has status information available, one of them is chosen
randomly, and its status is returned immediately. To get the status
from the other eligible child processes, you need to call the function again. The status information from the child process is stored in the object
that status-ptr points to, unless status-ptr is a null pointer.
The return value is normally the process ID of the child process whose
status is reported. If there are child processes but none of them is
waiting to be noticed, waitpid will block until one is. However,
if the WNOHANG option was specified, waitpid will return
zero instead of blocking. A value of -1 is returned in case of error.
8) The pid_t wait (int *status-ptr) function is a simplified version of waitpid, and is used to wait
until any one child process terminates. This function can be used a cancellation point in multi-threaded programs. This
is a problem if the thread allocates some resources (like memory, file
descriptors, semaphores or whatever) at the time wait is
called. If the thread gets canceled these resources stay allocated
until the program ends. To avoid this calls to wait should be
protected using cancellation handlers.
9) The difference lies in the fact that waitpid waits for the requested pid child to terminate, on-the-other-hand, wait resumes execution immediately after any of the child processes terminates.